﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Sockets;
using forum.Data;
using System.Text;
using System.IO;
using System.Threading;

namespace forum.ApplicationCode
{
    public class forumConnection
    {
        static char delimiter = '\x1';
        static string endOfMessage = "\n\0\n";
        static System.Net.Sockets.TcpClient clientSocket = null;
        public  static Boolean Connect()
        {
         /*   
         //  string b = "aa" + c;
          clientSocket = new System.Net.Sockets.TcpClient();
          clientSocket.Connect("192.168.1.103", 61613);
          return true;
        /*  NetworkStream serverStream = clientSocket.GetStream();
          byte[] outStream = System.Text.Encoding.ASCII.GetBytes("hallo\n\0\n\22");
         serverStream.Write(outStream, 0, outStream.Length);
         serverStream.Flush();
         */

           
          System.Threading.Thread thread = new System.Threading.Thread(readFromSocket);
          thread.Start();
          Console.Write("bb");
          return true;

        }

        private static void readFromSocket()
        {
            Console.Write("aa");
           /* using (StreamReader reader = new StreamReader(clientSocket.GetStream(), Encoding.UTF8))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {

                }
            }*/

            while (true)
            {

            }
            
        }
   
        public static void closeConnection()
        {
            clientSocket.Close();
        }


        public static void addThread(string forumId, string title, string body)
        {
            string message = "";
            message = "addThread" + delimiter + Membership.GetUserName() + delimiter + forumId + delimiter + title
                + delimiter + body + delimiter + endOfMessage;
           NetworkStream serverStream = clientSocket.GetStream();
           byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message);
         serverStream.Write(outStream, 0, outStream.Length);
         serverStream.Flush();
        }

        public static void editThread(string threadId, string title, string body)
        {
            using (forumDataContext data = new forumDataContext())
            {
                string forumId = data.sp_getThreadFullDetails(int.Parse(threadId)).FirstOrDefault().subForumId.ToString();
                string message = "";
                message = "EditMsg" + delimiter + Membership.GetUserName() + delimiter + forumId + delimiter +
                   threadId + delimiter + threadId + delimiter + title + delimiter + body + delimiter +"true"+delimiter + endOfMessage;
                NetworkStream serverStream = clientSocket.GetStream();
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message);
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }

        }

        public static void addPost(string threadId, string title, string body)
        {
            using (forumDataContext data = new forumDataContext())
            {
                string forumId = data.sp_getThreadFullDetails(int.Parse(threadId)).FirstOrDefault().subForumId.ToString();
                string message = "";
                message = "addPost" + delimiter + Membership.GetUserName() + delimiter + forumId + delimiter +
                    threadId + delimiter  + threadId + delimiter + title
                    + delimiter + body + delimiter +"true"+delimiter + endOfMessage;
                NetworkStream serverStream = clientSocket.GetStream();
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message);
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }
        }

        public static void addPostToPost(string postId, string title, string body)
        {
            using (forumDataContext data = new forumDataContext())
            {
                int threadId = data.sp_getPostFullDetails(int.Parse(postId)).FirstOrDefault().threadId;
                string forumId = data.sp_getThreadFullDetails(threadId).FirstOrDefault().subForumId.ToString();
                string message = "";
                message = "addPost" + delimiter + Membership.GetUserName() + delimiter + forumId + delimiter +
                    threadId.ToString() + delimiter + postId + delimiter + title
                    + delimiter + body + delimiter + "false" + delimiter + endOfMessage;
                NetworkStream serverStream = clientSocket.GetStream();
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message);
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }
        }


        public static void editPost(string postId, string title, string body)
        {
            using (forumDataContext data = new forumDataContext())
            {
                int threadId = data.sp_getPostFullDetails(int.Parse(postId)).FirstOrDefault().threadId;
                string forumId = data.sp_getThreadFullDetails(threadId).FirstOrDefault().subForumId.ToString();
                string message = "";
                message = "EditMsg" + delimiter + Membership.GetUserName() + delimiter + forumId + delimiter +
                    threadId.ToString() + delimiter + postId + delimiter + title
                    + delimiter + body + delimiter + "false" + delimiter + endOfMessage;
                NetworkStream serverStream = clientSocket.GetStream();
                byte[] outStream = System.Text.Encoding.ASCII.GetBytes(message);
                serverStream.Write(outStream, 0, outStream.Length);
                serverStream.Flush();
            }
        }

           
    }
}